import * as BABYLON from "babylonjs"; //全部引入
import getAnmKeys from "./getAnmKeys";
import getPath from "./getPath";

// 添加无人机
function addDrone(scene) {
  BABYLON.SceneLoader.ImportMesh(
    "",
    "static/model/drone/",
    "Drone_obj.obj",
    scene,
    newMeshes => {
      // Set the target of the camera to the first imported mesh
      //   newMeshes[0].position = new BABYLON.Vector3(3, 10, 0);
      //   console.log(this.myMaterial2, '???')
      //   newMeshes[0].material = this.myMaterial2;
      const drone = BABYLON.Mesh.MergeMeshes(newMeshes);
      drone.position = new BABYLON.Vector3(0, 3, 0);
      drone.scaling = new BABYLON.Vector3(0.6, 0.6, 0.6);
      // 高亮图层
      // let hl = new BABYLON.HighlightLayer("hl1", scene);
      // hl.addMesh(drone, BABYLON.Color3.Green());

      console.log(drone, "22");
      // let light = new BABYLON.PointLight(
      //   "pointLight",
      //   new BABYLON.Vector3(0, 0, 0),
      //   scene
      // );
      var light = new BABYLON.SpotLight(
        "spotlight",
        new BABYLON.Vector3(0, 0, 0),
        new BABYLON.Vector3(0, -1, 0),
        Math.PI / 3,
        2,
        scene
      );
      light.parent = drone;
      light.diffuse = new BABYLON.Color3(0.5, 1, 0);
      // light.specular = new BABYLON.Color3(1, 1, 0);
      light.range = 500;
      light.intensity = 5;
      addFlyLine(scene, drone);
    }
  );
}
// 飞行轨迹
function addFlyLine(scene, drone) {
  let xyPath = [
    [3, 5],
    [-4, 3],
    [-2.5, -3],
    [1, -3],
    [1, 5]
  ];
  let linePath = getPath(xyPath, 3);
  // 无人机飞行动画
  var animationFly = new BABYLON.Animation(
    "animationFly",
    "position",
    30,
    BABYLON.Animation.ANIMATIONTYPE_VECTOR3,
    BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE
  );
  // 动画帧数组
  let keys = getAnmKeys(linePath);
  animationFly.setKeys(keys);
  drone.animations = [];
  drone.animations.push(animationFly);
  let lastKey = 100 - 100 / linePath.length;
  // 参数5动画播放速度
  scene.beginAnimation(drone, 0, lastKey, true, 0.2);
  // const flyLine = BABYLON.MeshBuilder.CreateLines("flyLine", {
  //   points: linePath
  // });
  // flyLine.Color = new BABYLON.Color3(1, 0, 0);
}

export default addDrone;
